#ifdef MAODV

/*****************************************************************************
 *
 * Copyright (C) 2003 CSHCN, University of Maryland.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Authors: Dionysus Blazakis <dblaze@umd.edu>
 *          Jonathan Polley <jpolley@umd.edu>
 *          Dan Rusk <drusk@umd.edu>
 *          Manish Karir <karir@umd.edu>
 *
 *****************************************************************************/

#include "defs.h"
#include "debug.h"
#include "maodv_rreq.h"
#include "maodv_rrep.h"
#include "multicast_routing_table.h"
#include "aodv_socket.h"
#include "aodv_rreq.h"
#include "aodv_rrep.h"
#include "maodv_ext.h"

/*
 *  maodv_rreq_process( lots of stuff )
 *  Processes an rreq that has already been sanity checked and is
 *  headed for a multicast group.
 */
void maodv_rreq_process( RREQ *rreq, int rreqlen, u_int32_t ip_src,  
    u_int32_t ip_dst, int ip_ttl, unsigned int ifindex, 
    GRP_REBUILD_EXT *grb_ext, int size )
{
    mrt_table_t * mrt;
    rt_table_t * rev_rt;
    u_int32_t rreq_orig, rreq_dest, rreq_orig_seqno, rreq_dest_seqno;
    u_int32_t rreq_id, rreq_new_hcnt;
    mrt_nexthop_t * nh;
    mrt_orig_t *orig;
    
    DEBUG( LOG_DEBUG, 0, "Inside MADOV_RREQ process." );

    rreq_id = ntohl(rreq->rreq_id);
    rreq_dest = ntohl(rreq->dest_addr);
    rreq_dest_seqno = ntohl(rreq->dest_seqno);
    rreq_orig = ntohl(rreq->orig_addr);
    rreq_orig_seqno = ntohl(rreq->orig_seqno);
    rreq_new_hcnt = rreq->hcnt + 1;

    mrt = mrt_table_find( rreq_dest );
    if ( mrt==NULL ) {
        /* no route found */
        DEBUG(LOG_DEBUG, 0, "MRREQ: No mrt found." );
        mrt = mrt_table_insert( rreq_dest, 0, rreq_dest_seqno, 
            ifindex, 255, 0 );
    }

    /* add nexthop downstream */
    nh = mrt_nexthop_find( mrt, rreq_orig );
    if ( nh == NULL ) {
        mrt_nexthop_add( mrt, ip_src, MRT_DOWNSTREAM, rreq_dest_seqno );
    }

    /* add the originator to the originator list if not there already */
    orig=mrt_origlist_find(mrt, rreq_orig);
    if(orig==NULL){
        DEBUG(LOG_DEBUG, 0, "originator not in list, adding");
        orig=mrt_origlist_add(mrt, rreq_orig);
    }
    DEBUG(LOG_DEBUG, 0, "originator seqno: %d hcnt: %d", orig->grp_seqno, orig->grp_hcnt);

    if ( rreq->j ) {
        /* a Join request */
        DEBUG(LOG_DEBUG, 0, "MRREQ: [j] Join." );

        if ( !mrt->is_router) {
            DEBUG(LOG_DEBUG, 0, "MRREQ: [j] Found a mrt, but not on the tree.");
        } else if ( mrt->grp_seqno < rreq_dest_seqno ) {
            DEBUG(LOG_DEBUG, 0, "MRREQ: [j] Found a mrt, but it is old. %d < %d", mrt->grp_seqno, rreq_dest_seqno );
            mrt->grp_seqno = rreq_dest_seqno;
        } else {
            if(grb_ext==NULL || mrt->leader_hcnt<=grb_ext->grp_hcnt){
                DEBUG(LOG_DEBUG, 0, "MRREQ: [j] Should send RREP!" );

                /* send rrep */
                rev_rt = rt_table_find( rreq_orig );
                orig->grp_seqno=mrt->grp_seqno;
                orig->grp_hcnt=0;
                maodv_rrep_send( rev_rt, mrt );

                /* done! get out */
                return; 
            }else{
                DEBUG(LOG_DEBUG, 0, "had repair extension, bad hopcount, do nothing");
            }
        }
    } else {
        /* non-Join request */
        DEBUG(LOG_DEBUG, 0, "MRREQ: [] non-Join." );

        if ( mrt->grp_seqno < rreq_dest_seqno ) {
            DEBUG(LOG_DEBUG, 0, "MRREQ: [] Found a mrt, but it is old." );
            /* update sequence number in our table */
            mrt->grp_seqno = rreq_dest_seqno;
        } else if ( mrt->is_router ) {
            DEBUG(LOG_DEBUG, 0, "MRREQ: [] Should send RREP!" );

            /* send rrep */
            rev_rt = rt_table_find( rreq_orig );
            orig->grp_seqno=mrt->grp_seqno;
            orig->grp_hcnt=0;
            maodv_rrep_send( rev_rt, mrt );

            /* we're done! */
            return;
        }
    }

    /* if we get this far, no RREP has been sent. */

    /* now, forward RREQ */
    /* if rreq has unknown seqno and our table has a known one
       (not zero), then update packet seqno field. or if ours
       is just bigger.
       CHECK: this may not be the way to do this */
    if ( ( mrt->grp_seqno > rreq_dest_seqno ) || 
         ( rreq->u && mrt->grp_seqno ) ) {
        rreq->u = 0;
        rreq->dest_seqno = htonl( mrt->grp_seqno );
    }

   /* send it back out */
   maodv_rreq_forward( rreq, --ip_ttl, size );
}

/*
 *  maodv_rreq_forward( RREQ * rreq, int ttl, int size )
 *  Forwards a route request.
 */
void maodv_rreq_forward( RREQ * rreq, int ttl, int size )
{
    int i;
    /* FORWARD the RREQ if the TTL allows it. */
    DEBUG(LOG_DEBUG, 0, "[maodv] forwarding RREQ src=%s, rreq_id=%lu",
          ip_to_str(ntohl(rreq->orig_addr)), ntohl(rreq->rreq_id));
            
    /* Queue the received message in the send buffer */
    rreq = (RREQ *) aodv_socket_queue_msg((AODV_msg *) rreq, size);
     
    rreq->hcnt++;               /* Increase hopcount to account for
                                 * intermediate route */

    /* Send out on all interfaces */
    for (i = 0; i < MAX_NR_INTERFACES; i++) {
        if (!DEV_NR(i).enabled)
            continue;
        aodv_socket_send((AODV_msg *) rreq, AODV_BROADCAST, size, ttl,
                         &DEV_NR(i));
    }
}

#endif /* MAODV */
